<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        .mask {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.2);
            /* display: flex; */
            justify-content: center;
            align-items: center;
            display: none;
        }

        .login-form {
            width: 300px;
            height: 200px;
            background-color: #fff;
            box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
            border-radius: 4px;
            padding: 10px;
        }
    </style>
</head>

<body>
    <button id="btn-login">登录</button>
    <div class="mask">
        <form class="login-form">
            <h3>登录表单</h3>
            <div>
                <input type="text" placeholder="请输入账号">
            </div>
            <div>
                <input type="password" placeholder="请输入密码">
            </div>
            <div>
                <!-- 此处先放一个普通按钮 -->
                <button type="button" class="btn-submit">立即登录</button>
            </div>
        </form>
    </div>
    <script>
        // 获取相关元素的dom对象
        var btnLogin = document.getElementById('btn-login')
        var mask = document.getElementsByClassName('mask')[0]
        var form = mask.getElementsByTagName('form')[0]
        mask.onclick = function () {
            // 隐藏模态框
            this.style.display = 'none'
        }
        form.onclick = function (event) {
            // 阻止事件冒泡
            event.stopPropagation()
        }
        btnLogin.onclick = function () {
            // 显示模态框
            mask.style.display = 'flex'
        }
    </script>
</body>

</html>